package com.open.basic.util;

import com.open.basic.exception.BizException;

import java.io.*;


/**
 * @author: system
 * @Date: 2024/9/24 上午10:16
 * @Version: V1.0
 * @description: ->
 */

public class CloneUtil {
    
    private CloneUtil() {
    }
    
    public static <T> T deepClone(T from) {
        if (from == null) return null;
        T to = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(bos);) {
            oos.writeObject(from);
            oos.flush();
        } catch (IOException e) {
            throw new BizException(e);
        }
        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream((bos.toByteArray())));) {
            to = (T) ois.readObject();
            bos.close();
        } catch (IOException | ClassNotFoundException e) {
            throw new BizException(e);
        }
        return to;
    }
}
